﻿$.extend($.fn.validatebox.defaults.rules, {
    minLength: {
        validator: function (value, param) {
            return value.length >= param[0];
        },
        message: '长度不得小于 {0} 位'
    },
    maxLength: {
        validator: function (value, param) {
            return value.length <= param[0];
        },
        message: '长度不得大于 {0} 位'
    },
    equals: {
        validator: function (value, param) {
            return value == $(param[0]).val();
        },
        message: '两次输入内容不一致'
    },
    noEquals: {
        validator: function (value, param) {
            return value != $(param[0]).val();
        },
        message: '两次输入内容一致'
    },
    length: {
        validator: function (value, param) {
            var len = $.trim(value).length;
            return len >= param[0] && len <= param[1];
        },
        message: "内容长度介于{0}和{1}之间."
    },
    tel: {
        validator: function (value) {
            return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
        },
        message: "格式不正确,请使用下面格式:010-88888888"
    },
    mobile: {
        validator: function (value) {
            return /^(13|15|18)\d{9}$/i.test(value);
        },
        message: "手机号码格式不正确(正确格式如：13845666666)"
    },
    telOrMobile: {
        validator: function (value) {
            return /^(13|15|18)\d{9}$/i.test(value) || /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
        },
        message: "请填入手机或电话号码,如 13845666666 或 010-8888888"
    },
    idcard: {
        validator: function (value) {
            return /^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(value);
        },
        message: "身份证号码格式不正确"
    },
    currency: {
        validator: function (value) {
            return /^d{0,}(\.\d+)?$/i.test(value);
        },
        message: "货币格式不正确"
    },
    qq: {
        validator: function (value) {
            return /^[1-9]\d{4,9}$/i.test(value);
        },
        message: "QQ号码格式不正确"
    },
    chinese: {
        validator: function (value) {
            return /^[\u0391-\uFFE5]+$/i.test(value);
        },
        message: "请输入中文"
    },
    english: {
        validator: function (value) {
            return /^[A-Za-z]+$/i.test(value);
        },
        message: "请输入英文"
    },
    unnormal: {
        validator: function (value) {
            return /.+/i.test(value);
        },
        message: "输入值不能为空和包含其他非法字符"
    },
    username: {
        validator: function (value) {
            return /^[a-zA-Z][a-zA-Z0-9_]{5,15}$/i.test(value);
        },
        message: "用户名不合法(字母开头，允许6-16字节，允许字母数字下划线)"
    },
    zip: {
        validator: function (value) {
            return /^[1-9]\d{5}$/i.test(value);
        },
        message: "邮政编码格式不正确"
    },
    ip: {
        validator: function (value) {
            return /d+.d+.d+.d+/i.test(value);
        },
        message: "IP地址格式不正确"
    }
});

//为datagrid,treegrid增加表头菜单,用于显示或隐藏列
var CreateGridHeaderContextMenu = function (e, field) {
    e.preventDefault(); var grid = $(this), headerContextMenu = this.headerContextMenu;
    if (!headerContextMenu) {
        var tmenu = $("<div style='width:120px;'></div>").appendTo("body");
        var fields = grid.datagrid("getColumnFields");
        for (var i = 0; i < fields.length; i++) {
            var fildOption = grid.datagrid("getColumnOption", fields[i]);
            if (fildOption.title) {
                if (!fildOption.hidden) {
                    $("<div iconCls='tree-checkbox1' field='" + fields[i] + "'/>").html(fildOption.title).appendTo(tmenu);
                } else {
                    $("<div iconCls='tree-checkbox0' field='" + fields[i] + "'/>").html(fildOption.title).appendTo(tmenu);
                }
            }
        }
        headerContextMenu = this.headerContextMenu = tmenu.menu({
            onClick: function (item) {
                var field = $(item.target).attr("field");
                if (item.iconCls == "tree-checkbox1") {
                    grid.datagrid("hideColumn", field);
                    $(this).menu("setIcon", { target: item.target, iconCls: "tree-checkbox0" });
                } else {
                    grid.datagrid("showColumn", field);
                    $(this).menu("setIcon", { target: item.target, iconCls: "tree-checkbox1" });
                }
            }
        });
    }
    headerContextMenu.menu("show", { left: e.pageX, top: e.pageY });
};
$.fn.datagrid.defaults.onHeaderContextMenu = CreateGridHeaderContextMenu;
$.fn.treegrid.defaults.onHeaderContextMenu = CreateGridHeaderContextMenu;

//扩展tree,使其支持平滑数据格式（SyPro）
var EasyuiTreeLoadFilter = function (data, parent) {
    var treeData = [], tmpMap = [];
    for (var i = 0; i < data.length; i++) { tmpMap[data[i]["id"]] = data[i]; }
    for (var i = 0; i < data.length; i++) {
        if (tmpMap[data[i]["parent"]]) {
            if (!tmpMap[data[i]["parent"]]["children"]) {
                tmpMap[data[i]["parent"]]["children"] = [];
            }
            tmpMap[data[i]["parent"]]["children"].push(data[i]);
        } else { treeData.push(data[i]); }
    }
    return treeData;
};
$.fn.combotree.defaults.loadFilter = EasyuiTreeLoadFilter;
$.fn.tree.defaults.loadFilter = EasyuiTreeLoadFilter;

//扩展treegrid,使其支持平滑数据格式（SyPro）
var EasyuiTreeGridLoadFilter = function (data, parentId) {
    var opt = $(this).data().treegrid.options;
    var treeData = [], tmpMap = [];
    for (var i = 0; i < data.length; i++) { tmpMap[data[i][opt.idField]] = data[i]; }
    for (var i = 0; i < data.length; i++) {
        if (tmpMap[data[i][opt.parentField]]) {
            if (!tmpMap[data[i][opt.parentField]]["children"]) {
                tmpMap[data[i][opt.parentField]]["children"] = [];
            }
            tmpMap[data[i][opt.parentField]]["children"].push(data[i]);
        } else { treeData.push(data[i]); }
    }
    return treeData;
};
$.fn.treegrid.defaults.loadFilter = EasyuiTreeGridLoadFilter;

//防止panel,window,dialog组件超出浏览器边界
var EasyuiPanelOnMove = function (left, top) {
    if (left < 1) { left = 1; }
    if (top < 1) { top = 1; }

    var width = parseInt($(this).parent().css('width')) + 14;
    var height = parseInt($(this).parent().css('height')) + 14;

    var right = left + width;
    var buttom = top + height;

    var browserWidth = $(window).width();
    var browserHeight = $(window).height();

    if (right > browserWidth) { left = browserWidth - width; }

    if (buttom > browserHeight) { top = browserHeight - height; }

    $(this).parent().css({ left: left, top: top });
};
$.fn.dialog.defaults.onMove = EasyuiPanelOnMove;
$.fn.window.defaults.onMove = EasyuiPanelOnMove;
$.fn.panel.defaults.onMove = EasyuiPanelOnMove;

//卸载时回收内存，主要用于 layout、panel(及其继承组件) 使用 iframe 嵌入网页时的内存泄漏问题
var EasyuiPanelonBeforeDestroy = function () {
    $("iframe,frame", this).each(function () {
        try {
            if (this.contentWindow && this.contentWindow.close) {
                this.contentWindow.document.write("");
                this.contentWindow.close();
            }
            $(this).remove();
            if ($.isFunction(window.CollectGarbage)) { window.CollectGarbage(); }
        } catch (ex) { }
    });
};
$.fn.panel.defaults.onBeforeDestroy = EasyuiPanelonBeforeDestroy;
$.fn.window.defaults.onBeforeDestroy = EasyuiPanelonBeforeDestroy;
$.fn.dialog.defaults.onBeforeDestroy = EasyuiPanelonBeforeDestroy;
$.fn.datagrid.defaults.onBeforeDestroy = EasyuiPanelonBeforeDestroy;
$.fn.propertygrid.defaults.onBeforeDestroy = EasyuiPanelonBeforeDestroy;
$.fn.treegrid.defaults.onBeforeDestroy = EasyuiPanelonBeforeDestroy;

//控件的一些加载错误的提示
var EasyuiErrorFunction = function (XMLHttpRequest) {
    $.Error(XMLHttpRequest.responseText);
};
$.fn.datagrid.defaults.onLoadError = EasyuiErrorFunction;
$.fn.treegrid.defaults.onLoadError = EasyuiErrorFunction;
$.fn.tree.defaults.onLoadError = EasyuiErrorFunction;
$.fn.combogrid.defaults.onLoadError = EasyuiErrorFunction;
$.fn.combobox.defaults.onLoadError = EasyuiErrorFunction;
$.fn.form.defaults.onLoadError = EasyuiErrorFunction;